package tax;

import org.apache.commons.collections.FastArrayList;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Calculator {

    private static final List<FeeStrategy> feeStrategyList;

    static {
        /**
         * 生成阶段收费基本条件，可改为从数据库获取
         */
        FeeStrategy fs1 = new FeeStrategy(0,100,0.01d,0d);
        FeeStrategy fs2 = new FeeStrategy(100,200,0.02d,0d);
        FeeStrategy fs3 = new FeeStrategy(200,500,0.03d,0d);
        FeeStrategy fs4 = new FeeStrategy(500,1000,0.04d,0d);
        FeeStrategy fs5 = new FeeStrategy(1000,2000,0.05d,0d);
        FeeStrategy fs6 = new FeeStrategy(2000,5000,0.06d,0d);
        FeeStrategy fs7 = new FeeStrategy(5000,Integer.MAX_VALUE,0.01d,0d);

        feeStrategyList = new ArrayList<FeeStrategy>();
        feeStrategyList.add(fs1);
        feeStrategyList.add(fs2);
        feeStrategyList.add(fs3);
        feeStrategyList.add(fs4);
        feeStrategyList.add(fs5);
        feeStrategyList.add(fs6);
        feeStrategyList.add(fs7);
        Collections.sort(feeStrategyList);
    }
    public double calculate(double amount){
        double tmp = 0d;
        for (FeeStrategy fs: feeStrategyList){
            System.out.println(String.format("左区间： %s, 右区间: %s.", fs.getLeft(), fs.getRight()));
            tmp += fs.fee(amount);
            if (amount <= fs.getRight()){
                break;
            }
        }
        return  amount - tmp;
    }

    public static void main(String[] args) {
        Calculator c = new Calculator();
        double input = 1280d;
        double result = c.calculate(input);
        System.out.println(String.format("Input Amount: %s, after taxed amount: %s.", input, result));
    }
}
